<template>
	<view class="member">
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
			:down="downOption" :up="upOption">
			<block v-for="(item,index) in list" :key="index">
				<view class="member_item" @click="selectMember(item)">
					<view class="member_info" v-if="item.limit == item.monthNums">
						<radio :disabled="true" 
							style="transform: scale(.8);flex-shrink: 0;margin-right: 8rpx;" color="#F12F63" />
						
						<view class="member_text">
							<!-- <view style="margin-bottom: 12rpx;">本月已使用 <text style="color: #F12F63; font-size: 36rpx;">{{item.monthNums}} </text>升</view>
							<view>每月限 <text style="color: #F12F63; font-size: 36rpx;">{{item.limit}}</text>  升</view> -->
							<view style="margin-bottom: 12rpx;">本月可使用 <text
									style="color: #F12F63; font-size: 36rpx;">{{item.balanceNums}} </text>升</view>
							<view class="">
								<text>本轮周期 {{item.epicycle}}</text>
							</view>
							<text>有效时间 {{item.expires_time}}</text>
							<view class="" style="font-size: 22rpx;" v-if="item.station_name">
								仅限<text style="color: #F12F63; font-size:22rpx;">{{item.station_name}}</text>使用
							</view>
						</view>
					</view>
					<view class="member_info" v-else>
						<radio :checked="isChecked(item.id)"
							style="transform: scale(.8);flex-shrink: 0;margin-right: 8rpx;" color="#F12F63" />
						
						<view class="member_text">
							<!-- <view style="margin-bottom: 12rpx;">本月已使用 <text style="color: #F12F63; font-size: 36rpx;">{{item.monthNums}} </text>升</view>
							<view>每月限 <text style="color: #F12F63; font-size: 36rpx;">{{item.limit}}</text>  升</view> -->
							<view style="margin-bottom: 12rpx;">本月可使用 <text
									style="color: #F12F63; font-size: 36rpx;">{{item.balanceNums}} </text>升</view>
							<view class="">
								<text>本轮周期 {{item.epicycle}}</text>
							</view>
							<text>有效时间 {{item.expires_time}}</text>
							<view class="" style="font-size: 22rpx;" v-if="item.station_name">
								仅限<text style="color: #F12F63; font-size:22rpx;">{{item.station_name}}</text>使用
							</view>
						</view>
					</view>
					
					
					<view class="member_money">
						<text>剩余</text>
						<view class="oil-item-data">
							<text>￥</text>
							{{(item.money)}}
							<text style="color: #F12F63;">元</text>
						</view>
					</view>
				</view>
			</block>
			<mescroll-empty v-if="list.length==0"></mescroll-empty>
		</mescroll-body>
		
		<view class="member-confirm">
			<view class="member-btn" @click="checkedMember()">
				<text>确定</text>
			</view>
		</view>
	</view>
</template>

<script>
	import api from '@/api/mine.js';
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				list: [],
				id: '',
				member: {},
				showRadio: false, // 默认radio不显示，当加油需要选择优惠券时显示radio
				downOption: {
					use: true,
				},
				upOption: {
					use: true,
					auto: true,
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 15, // 每页数据的数量
						time: null // 加载第一页数据服务器返回的时间; 防止用户翻页时,后台新增了数据从而导致下一页数据重复;
					},
					empty: {
						use: false, // 是否显示空布局
						icon: "https://www.mescroll.com/img/mescroll-empty.png", // 图标路径
						tip: '~ 暂无相关数据 ~', // 提示
						btnText: '去逛逛 >', // 按钮
						fixed: false, // 是否使用fixed定位,默认false; 配置fixed为true,以下的top和zIndex才生效 (transform会使fixed失效,最终会降级为absolute)
						top: "100rpx", // fixed定位的top值 (完整的单位值,如 "10%"; "100rpx")
						zIndex: 99 // fixed定位z-index值
					}
				},
			};
		},
		
		onLoad(options) {
			this.id = options.id
		},
		
		computed: {
			isChecked(){
				return function(id){
					return this.member.id*1 === id*1
				};
			}
		},
		
		methods: {
			downCallback() {
				this.mescroll.resetUpScroll();
			},
			
			upCallback(page) {
				let data = {
					page: page.num,
					station_id: this.id
				};
				let _this = this;
				api.getMember(data).then(res => {
					//设置列表数据
					if (page.num == 1) this.list = [];
					// let data = JSON.parse(JSON.stringify(res.data.data))
					// data.forEach((item, index) => {
					// 		item.showRadio = false;
					// 	})
					// const filterData = Object.values(data);
					_this.list = _this.list.concat(res.data.data);
					// console.log(_this.list,data);
					this.mescroll.endBySize(res.data.data.length, res.data.total);
					if(_this.list.length > 0){
						_this.member = _this.list[0];
					}else{
						_this.member = null;
					};
				}).catch(() => {
					this.mescroll.endErr();
				});
			},
			
			checkedMember(){
				let _this = this
				if(_this.member.balanceNums == 0){
					_this.member = {}
					uni.navigateBack({
						delta: 1,
						success() {
							uni.$emit('member_info', _this.member);
						}
					});
				}else{
					uni.navigateBack({
						delta: 1,
						success() {
							uni.$emit('member_info', _this.member);
						}
					});
				}
				console.log(_this.member);
			},
			
			selectMember(item) {
				let arr = {}
				arr = item
				this.member = arr
				item.showRadio = !item.showRadio;
				if(item.showRadio == false || item.balanceNums == 0){
					this.member = {}
				}else{
					this.isChecked()
				}
			}
		}
	}
</script>

<style lang="scss">
	.member{
		background-color: #F3F3F3;
		.member_item{
			min-height: 200rpx;
			display: flex;
			align-items: center;
			border-radius: 20rpx;
			background-color: #FFF;
			overflow: hidden;
			margin: 30rpx 30rpx 0;
			box-sizing: border-box;
			
			.member_money{
				min-height: 200rpx;
				width: 30%;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				background-color: #fff;
				border-radius: 0 16rpx 16rpx 0;
				border-left: 2rpx dashed #e5e5e5;
				box-sizing: border-box;
				.oil-item-data {
					color: #F12F63;
					font-size: 40rpx;
					font-weight: bold;
				
					text {
						font-size: 30rpx;
					}
				}
			}
			
			.member_info{
				flex: 1;
				height: 100%;
				background-color: #fff;
				display: flex;
				align-items: center;
				padding: 10rpx 30rpx;
				border-radius: 16rpx 0 0 16rpx;
				box-sizing: border-box;
				.member_text{
					display: flex;
					flex-direction: column;
					font-size: 24rpx;
					text{
						margin: 12rpx 0;
					}
				}
			}
		}
		.member-confirm {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			padding: 20rpx;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			background-color: #fff;
			box-sizing: border-box;
			.member-btn {
				width: 180rpx;
				height: 64rpx;
				line-height: 64rpx;
				background-color: #F12F63;
				font-size: 28rpx;
				color: #fff;
				text-align: center;
				border-radius: 8rpx;
			}
		}
	}
</style>
